﻿/*
 * @Author: ws.s 
 * @Date: 2021-05-25 21:21:09 
 * @Last Modified by: ws.s
 * @Last Modified time: 2021-05-26 10:57:33
 */
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


/// <summary>
/// Atlas 动画，适用于 脚本制作简单的帧动画
/// </summary>
public class BhvAtlasAnimate : BhvAtlasFrame
{
    public SpriteRenderer spriteRenderer;
    public Dictionary<string, AnimationClip> animateDic = new Dictionary<string, AnimationClip>();
    public float speed = 1;
    private float timer = 0;

    public bool isPlaying = false;

    private int animateIndex = 0;

    private AnimationClip currentClip;

    public class AnimationClip
    {
        public string name = "";
        /// <summary>
        /// 动画帧
        /// </summary>
        public IList<string> frames;

        /// <summary>
        /// 每帧播放时间
        /// </summary>
        public float frameTime = 1 / 60F;

        public bool isLoop = false;
    }

    /// <summary>
    /// 添加动画
    /// </summary>
    /// <param name="key">动画key</param>
    /// <param name="ani">动画序列帧名</param>
    /// <param name="duration">动画持续时间</param>
    /// <param name="isLoop">是否循环</param>
    public void AddAnimation(string key, IList<string> ani, float duration = 1f, bool isLoop = false)
    {
        var clip = new AnimationClip();
        clip.frames = ani;
        clip.frameTime = duration / ani.Count;
        clip.isLoop = isLoop;
        animateDic.Add(key, clip);
    }

    /// <summary>
    /// 移除动画
    /// </summary>
    /// <param name="key"></param>
    public void RemoveAnimation(string key)
    {
        if (animateDic.ContainsKey(key))
        {
            animateDic.Remove(key);
        }
    }

    public void ClearAll()
    {
        animateDic.Clear();
    }

    /// <summary>
    /// 播放动画
    /// </summary>
    /// <param name="clipName"></param>
    /// <param name="speed"></param>
    public void Play(string clipName, float speed = 1)
    {
        if (animateDic.ContainsKey(clipName) == false) return;
        if (currentClip != null && currentClip.name == clipName)
        {
            currentClip = animateDic[clipName];
        }
        else
        {
            currentClip = animateDic[clipName];
        }
        this.animateIndex = 0;
        this.frameName = currentClip.frames[0];
        this.speed = speed;
        this.isPlaying = true;
    }

    public void Stop()
    {
        isPlaying = false;
    }

    public void MoveNextFrame()
    {
        if (this.currentClip == null)
        {
            return;
        }

        this.animateIndex++;
        int frameCount = this.currentClip.frames.Count;
        if (this.currentClip.isLoop)
        {
            animateIndex = animateIndex % frameCount;
        }
        else
        {
            if (animateIndex >= frameCount)
            {
                this.Stop();
            }
        }

        var sprite = this.currentClip.frames[animateIndex];
        this.frameName = sprite;

    }

    private void Update()
    {
        if (!isPlaying || this.currentClip == null) return;

        // MoveSpeed
        timer += Time.deltaTime * speed;
        if (timer >= this.currentClip.frameTime)
        {
            timer -= this.currentClip.frameTime;
            MoveNextFrame();
        }

    }


}
